home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Utilities / MView / gxu / filestrm.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-09-30  |  2.8 KB  |  133 lines

  1. /*//////////////////////////////////////////////////////////////////////////////
  2. //
  3. // File: filestrm.cpp
  4. //
  5. // Copyright (C) 1999 Microsoft Corporation. All Rights Reserved.
  6. //
  7. //
  8. //////////////////////////////////////////////////////////////////////////////*/
  9. #include "pchgxu.h"
  10.  
  11. #include "Filestrm.h"
  12.  
  13. CFileStream::CFileStream(LPCTSTR filename, BOOL bRead, BOOL bTruncate, HRESULT *error)
  14. {
  15.     m_hfile = CreateFile(filename, bRead ? GENERIC_READ : GENERIC_WRITE, 0, NULL, 
  16.                           (bTruncate ? CREATE_ALWAYS : OPEN_EXISTING), FILE_ATTRIBUTE_NORMAL,
  17.                           NULL);
  18.     if (error)
  19.     {
  20.         ULONG foo = GetLastError();
  21.         if (m_hfile == INVALID_HANDLE_VALUE)
  22.             *error = E_FAIL;
  23.         else
  24.             *error = NOERROR;
  25.     }
  26.     m_cRef = 1;
  27. }
  28.  
  29. CFileStream::~CFileStream()
  30. {
  31.     CloseHandle(m_hfile);
  32. }
  33.  
  34. STDMETHODIMP_(ULONG) CFileStream::AddRef(void)
  35. {
  36.     return m_cRef++;    
  37. }
  38.  
  39. STDMETHODIMP_(ULONG) CFileStream::Release(void)
  40. {
  41.     if (--m_cRef != 0)    
  42.         return m_cRef;    
  43.     
  44.     delete this;
  45.     return 0;
  46. }
  47.  
  48.  
  49. STDMETHODIMP CFileStream::QueryInterface(REFIID riid, LPVOID FAR *ppv)
  50. {    
  51.     *ppv=NULL;
  52.     if (riid == IID_IUnknown)
  53.         *ppv=(IUnknown*)this;
  54.     else if (riid == IID_IStream)
  55.         *ppv=(IStream*)this;
  56.     else
  57.         return E_NOINTERFACE;
  58.     ((LPUNKNOWN)*ppv)->AddRef();
  59.     return NOERROR;
  60. }
  61.  
  62.  
  63. STDMETHODIMP CFileStream::Read(void __RPC_FAR *pv, ULONG cb, ULONG __RPC_FAR *pcbRead)
  64. {
  65.     DWORD read;
  66.     BOOL result = ReadFile(m_hfile, pv, cb, &read, NULL);
  67.     if (pcbRead)
  68.         *pcbRead = read;
  69.     if (result)
  70.         return S_OK;
  71.     else
  72.         return E_FAIL;
  73. }
  74.  
  75.  
  76. STDMETHODIMP CFileStream::Write(const void __RPC_FAR *pv, ULONG cb, ULONG __RPC_FAR *pcbWritten)
  77. {
  78.     DWORD written;
  79.     BOOL result = WriteFile(m_hfile, pv, cb, &written, NULL);
  80.     if (pcbWritten)
  81.         *pcbWritten = written;
  82.     if (result)
  83.         return S_OK;
  84.     else
  85.         return E_FAIL;
  86. }
  87.  
  88. STDMETHODIMP CFileStream::Seek(LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER __RPC_FAR *plibNewPosition)
  89. {
  90.     LONG movelow;
  91.     LONG movehigh;
  92.  
  93.     movelow = dlibMove.LowPart;
  94.     movehigh = dlibMove.HighPart;
  95.  
  96.     DWORD moveMethod;
  97.     switch (dwOrigin)
  98.     {
  99.     case STREAM_SEEK_SET: moveMethod = FILE_BEGIN; break;
  100.     case STREAM_SEEK_CUR: moveMethod = FILE_CURRENT; break;
  101.     case STREAM_SEEK_END: moveMethod = FILE_END; break;
  102.     default: return E_INVALIDARG;
  103.     }
  104.  
  105.  
  106.  
  107.     DWORD result = SetFilePointer(m_hfile, movelow, &movehigh, moveMethod);
  108.  
  109.     if (plibNewPosition)
  110.     {
  111.         plibNewPosition->LowPart = result;
  112.         plibNewPosition->HighPart = movehigh;
  113.     }
  114.  
  115.     if (result != -1)
  116.         return NOERROR;
  117.     else
  118.         return E_FAIL;
  119. }
  120.  
  121.  
  122. STDMETHODIMP CFileStream::Stat(STATSTG __RPC_FAR *pstatstg, DWORD grfStatFlag)
  123. {
  124.     memset(pstatstg, 0, sizeof(STATSTG));
  125.  
  126.     pstatstg->pwcsName = NULL; 
  127.     pstatstg->type = STGTY_STREAM; 
  128.  
  129.     pstatstg->cbSize.LowPart = GetFileSize(m_hfile, &pstatstg->cbSize.HighPart); 
  130.  
  131.     return S_OK;
  132. }
  133.